package com.example.echoewish.ui.my_activity.my_publish_activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;

import com.example.echoewish.R;
import com.example.echoewish.pojo.MyPublishPost;
import com.example.echoewish.econstant.Constant;
import com.example.echoewish.ui.EchoWishCompatActivity;

import java.util.LinkedList;
import java.util.List;

public class MyPublishActivity extends EchoWishCompatActivity {


    private ListView myPublishListView;
    private MyPublishPostAdapter myPublishPostAdapter;

    private MyPublishViewModel myPublishViewModel;
    private int nowPosition;

    private Handler myPublishandler =new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what)
            {
                case MyPublishViewModel.CHANGE_LIST_VIEW:
                    myPublishPostAdapter.clear();
                    myPublishPostAdapter.addAll(myPublishViewModel.myPublishPostMutableLiveData.getValue());
                    myPublishPostAdapter.notifyDataSetChanged();
                    break;
                case Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE:
                    Toast.makeText(MyPublishActivity.this,"网络连接失败 请检查网络",Toast.LENGTH_SHORT).show();
                    break;
                case MyPublishViewModel.DELETE_POST_FAIL:
                    Toast.makeText(MyPublishActivity.this,"连接失败",Toast.LENGTH_SHORT).show();
                    break;
                case MyPublishViewModel.DELETE_POST_SUCCESS:
                    Toast.makeText(MyPublishActivity.this,"删除成功",Toast.LENGTH_SHORT).show();
                    myPublishPostAdapter.remove(myPublishPostAdapter.getItem(nowPosition));
                    myPublishPostAdapter.notifyDataSetChanged();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        myPublishViewModel.getMyPublishByUserId();
        myPublishPostAdapter.setButtonClickListener(new MyPublishPostAdapter.ButtonClickListener() {
            @Override
            public void buttonClickListener(int nowPosition) {
                MyPublishActivity.this.nowPosition=nowPosition;
                myPublishViewModel.deletePostIdMutableLiveData.setValue(myPublishPostAdapter.getItem(nowPosition).postId);
            }
        });
    }

    @Override
    public void setContentView() {
        setContentView(R.layout.mypublish_layout);
    }

    public void initView()
    {
        myPublishListView=findViewById(R.id.my_publish_listview);
        myPublishPostAdapter=new MyPublishPostAdapter(this,R.layout.my_publish_item,new LinkedList<>());
        myPublishListView.setAdapter(myPublishPostAdapter);
    }

    @Override
    public void initViewModel() {
        myPublishViewModel=new ViewModelProvider(this,new ViewModelProvider.AndroidViewModelFactory(getApplication())).get(MyPublishViewModel.class);
    }

    @Override
    public void observeLiveData() {

        myPublishViewModel.myPublishPostMutableLiveData.observe(this, new Observer<List<MyPublishPost>>() {
            @Override
            public void onChanged(List<MyPublishPost> myPublishPost) {
                if(myPublishPost!=null)
                {
                    Message message=new Message();
                    message.what= myPublishViewModel.CHANGE_LIST_VIEW;
                    myPublishandler.sendMessage(message);
                }
                else
                {
                    Message message=new Message();
                    message.what= Constant.HTTPS_GET_CALLBACK_FAIL_MESSAGE;
                    myPublishandler.sendMessage(message);
                }
            }
        });

        myPublishViewModel.deletePostIdMutableLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                myPublishViewModel.deletePostByPostId();
            }
        });

        myPublishViewModel.responseMutableLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String s) {
                Message message=new Message();
                if (s.equals(Constant.HTTPS_GET_CALLBACK_SUCCESS))
                {
                    message.what=MyPublishViewModel.DELETE_POST_SUCCESS;
                    myPublishandler.sendMessage(message);
                }
                else
                {
                    message.what=MyPublishViewModel.DELETE_POST_FAIL;
                    myPublishandler.sendMessage(message);
                }
            }
        });
    }


}
